package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.RegistrationFeeDto;
import xyz.zhouzhaodong.crm.entity.RegistrationFee;
import xyz.zhouzhaodong.crm.mapper.RegistrationFeeMapper;
import xyz.zhouzhaodong.crm.service.IRegistrationFeeService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 报名费，高校部自己维护 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-29
 */
@Service
public class RegistrationFeeServiceImpl extends ServiceImpl<RegistrationFeeMapper, RegistrationFee> implements IRegistrationFeeService {
    
    @Resource
    RegistrationFeeMapper registrationFeeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addRegistrationFee(RegistrationFee registrationFee) {
        try {
            // 新增
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            registrationFee.setId(id);
            registrationFee.setCreateUser(registrationFee.getStaffId());
            registrationFee.setCreateTime(new Date());
            registrationFeeMapper.insert(registrationFee);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteRegistrationFee(RegistrationFee registrationFee) {
        try {
            RegistrationFee r = new RegistrationFee();
            r.setId(registrationFee.getId());
            r.setFlag(1);
            r.setUpdateUser(registrationFee.getStaffId());
            r.setUpdateTime(new Date());
            registrationFeeMapper.updateById(r);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateRegistrationFee(RegistrationFee registrationFee) {
        try {
            registrationFee.setUpdateUser(registrationFee.getStaffId());
            registrationFee.setUpdateTime(new Date());
            registrationFee.setStaffId(null);
            registrationFeeMapper.updateById(registrationFee);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectRegistrationFee(RegistrationFeeDto registrationFeeDto) {
        Page<RegistrationFee> page = new Page<>(registrationFeeDto.getPageNo(),
                registrationFeeDto.getPageSize() == 0 ? 10 :registrationFeeDto.getPageSize());
        QueryWrapper<RegistrationFee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag", registrationFeeDto.getFlag() == null ? 0 :registrationFeeDto.getFlag());
        if(registrationFeeDto.getId() != null && !Objects.equals(registrationFeeDto.getId(), "")){
            queryWrapper.eq("id", registrationFeeDto.getId());
        }
        if(registrationFeeDto.getNumber() != null && !Objects.equals(registrationFeeDto.getNumber(), "")){
            queryWrapper.like("number", registrationFeeDto.getNumber());
        }
        if(registrationFeeDto.getSchool() != null && !Objects.equals(registrationFeeDto.getSchool(), "")){
            queryWrapper.like("school", registrationFeeDto.getSchool());
        }
        if(registrationFeeDto.getOperationTime() != null && !Objects.equals(registrationFeeDto.getOperationTime(), "")){
            queryWrapper.eq("operation_time", registrationFeeDto.getOperationTime());
        }
        queryWrapper.orderByDesc("operation_time");
        IPage<RegistrationFee> list = registrationFeeMapper.selectPage(page, queryWrapper);
        return RestMessage.success(list);
    }
}
